//
//  UserAccount.swift
//  新浪微博
//
//  Created by mac on 15/11/28.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    //用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    //access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet{
       expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    var expires_date: NSDate?
    //access_token的生命周期（该参数即将废弃，开发者请使用expires_in）
    var remind_in: String?
    //当前授权用户的UID
    var uid: String?
    //友好显示名称
    var name: String?
    //用户头像地址,大图 180*180像素
    var avatar_large: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //重写description 属性
   override var description: String {
        let keys = ["access_token","expires_in","uid","avatar_large","expires_date"]
    return dictionaryWithValuesForKeys(keys).description
    }
    //实现nscoding协议方法
    //接归档,将二进制数据转换成对象 反序列化相似
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token")as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date")as? NSDate
        name = aDecoder.decodeObjectForKey("name")as? String
        uid = aDecoder.decodeObjectForKey("uid")as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large")as? String
    }
    //归档,将对象转换成二进制数据存放在沙盒路径
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
      
    }
    //保存
    func saveAccount(){
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
    }
    //从磁盘中,获取自定义对象
    //class func 和oc 中的类方法一样
    class func loadAccount() -> UserAccount? {
    //接归档数据
         let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
//        print(path)
//        //解归档
//        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
//            
//            //添加过期日期判断
//            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
//                return account
//            }

       if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount{
        if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
//            print("-------")
        return account
        }
    }
       return nil
    }
}
